<?php

// 分销商

namespace app\shop\controller;

use think\Request;
use think\Session;
use think\Controller;
use app\shop\model\MemberDistribution as MemberDistributionModel;

class Distribution extends Controller
{
    /**
     * 我的分销
     */
    public function index()
    {
        return $this->fetch('');
    }


    /**
     * 我的分销商
     * @param Request $request
     * @param MemberDistributionModel $memberDistribution
     * @return mixed
     */
    public function businessman(Request $request,MemberDistributionModel $memberDistribution)
    {
        //会员id(分销人) 层级浮动值
        $memberId = Session::get('member_id');
        $level_float = $request->param('level',1);


        //查找此会员的级别定位
        $level = $memberDistribution->where('referrals_id',$memberId)->value('level');

        //老板登录
        // if ($memberId == 1) {$level = 1 ;}

        // 级别数量 (下线人)匹配会员表
        $rank['one_level'] = $memberDistribution->where(['hierarchy' => ['like', '%,' . $memberId . ',%'], 'level' => 2])->with('member')->count();
        $rank['two_level'] = $memberDistribution->where(['hierarchy' => ['like', '%,' . $memberId . ',%'], 'level' => 3])->with('member')->count();
        $level += $level_float;

        // if ($memberId == 6524){ $level -= 1 ;} //老板登录 层级降1

        // 搜索条件
        $condition['sun_member_distribution.hierarchy'] = ['like', '%,'.$memberId.',%'];
        if (!empty($level)) $condition['sun_member_distribution.level'] = ['eq', $level];

        $query = $memberDistribution->where($condition)->with('member,memberDistributionOrder,TjMember')->order('create_time desc')->select();

        //整理佣金
        $result = [];
        if ($query){
            foreach ($query as $key => &$value){
                $result[$key]['avatar'] = $value->member->avatar;
                $result[$key]['nickname'] = $value->member->nickname;
                $result[$key]['username'] = $value->member->username;
                $result[$key]['TjNickname'] = $value->tjMember->nickname;
                $result[$key]['create_time'] = $value->create_time;
                $result[$key]['brokerage'] = "0.00";

                //分销订单
                if ($value->memberDistributionOrder){
                    //拓展符合条件的会员订单
                    foreach ($value->memberDistributionOrder as $item){
                        $item->memberOrder;
                    }
                    //整理佣金
                    foreach ($value->memberDistributionOrder as $k => $_item){
                        //合计有订单数据的佣金
                        if (!is_null($_item->memberOrder)){
                            $result[$key]['brokerage'] += $_item['brokerage'];
                        }
                    }
                }
            }
        }

        // 数组排序
        if (!empty($result)) {
            foreach ( $result as $key => $row ) {
                $num1[$key] = $row ['brokerage'];
            }
            array_multisort($num1,SORT_DESC,$result);
        }


        return $this->fetch('',[
            'mate_head' => '分销商',
            'rank'      => $rank,
            'data'      => $result,
        ]);
    }
}